<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use doumiaoduo\layer\LayerAsset;
$cash = 0;
$p_cash = 0;
$p_name = '';
if(isset($account)){
    if($goods->category_id == 1){
        if($account->puzzle_cash > 0){
            if($account->puzzle_cash > $model->total_amount){
                $p_cash = $model->total_amount;
            }else{
                $p_cash = $account->puzzle_cash;
            }
         }
        $p_name = '益智专用金额';
    }
    if($goods->category_id == 2){
        if($account->music_cash > 0){
            if($account->music_cash > $model->total_amount){
                $p_cash = $model->total_amount;
            }else{
                $p_cash = $account->music_cash;
            }
        }
        $p_name = '音乐专用金额';
    }
    if($goods->category_id == 3){
        if($account->body_cash > 0){
            if($account->body_cash > $model->total_amount){
                $p_cash = $model->total_amount;
            }else{
                $p_cash = $account->body_cash;
            }
        }
        $p_name = '形体专用金额';
    }

    if($account->cash >0 && $model->total_amount-$p_cash > 0){
        if($account->cash > $model->total_amount-$p_cash)
         $cash = $model->total_amount-$p_cash;
        else
         $cash =  $account->cash;
    }
}

?>
<div class="yx-cnt-box yx-praise-list-box yx-praise-box yx-radius2 zx-min-height">
    <div class="zx-mg">
        <?php $form = ActiveForm::begin(['enableAjaxValidation' => true,'enableClientValidation' => false,'options'=>['class'=>'form-validate']]); ?>
        <table id="data-load" class="yx-course-info yx-course-info1 yx-course-info2" >
                <tr>
                    <td>订单总金额：</td>
                    <td><?=$model->total_amount?></td>
                </tr>
                <tr>
                    <td>奖学金：</td>
                    <td><?=($account)?$account->scholarship_available:0?></td>
                </tr>
                <tr>
                    <td>可使用奖学金：</td>
                    <td>
                        <?php if($available_scholarship > 0) { ?><?= \yii\helpers\Html::radio('discount',false,['data'=>['type'=>'scholarship'],'value'=>$available_scholarship,'class'=>'input-radiolist'])?><?php } ?><?=$available_scholarship?></td>
                </tr>
                <?php if($goods->category_id == 1) {?>
                <tr>
                    <td>可使用益智类金额：</td>
                    <td>
                        <?=($account)?$account->puzzle_cash:0?></td>
                </tr>
            <?php } ?>
            <?php if($goods->category_id == 2) {?>
                <tr>
                    <td>可使用音乐类金额：</td>
                    <td>
                        <?=($account)?$account->music_cash:0?></td>
                </tr>
            <?php } ?>
            <?php if($goods->category_id == 3) {?>
                <tr>
                    <td>可使用形体类金额：</td>
                    <td>
                        <?=($account)?$account->body_cash:0?></td>
                </tr>
            <?php } ?>
                <?php if($coupons):?>
                    <?php foreach($coupons as $coupon):?>
                    <tr>
                        <td>优惠券：</td>
                        <td><?= \yii\helpers\Html::radio('discount',false,['data'=>['type'=>'coupon'],'value'=>$coupon['id'],'class'=>'input-radiolist'])?><?=$coupon['name']?></td>
                    </tr>
                     <?php endforeach?>
                <?php endif;?>
                <tr>
                    <td>账户余额：</td>
                    <td><?php if(isset($account) && $account->cash >0) {?><?= \yii\helpers\Html::checkbox('cash',true,['data'=>['type'=>'cashs'], 'value'=>isset($account)?$account->cash:0,'class'=>'input-radiolist','style'=>'display:none'])?><?php }?><?=isset($account)?$account->cash:0?></td>
                </tr>

                <tr id="is_bond" style="display: none">
                    <td>保证金：</td>
                    <td><?=number_format($deposit,2)?></td>
                </tr>
            <?php if($availableDeposit != null) { ?>
                <tr id="is_deposit" style="display: none">
                    <td>转保证金：</td>
                    <td>
                        <table width="100%">
                            <?php foreach($availableDeposit as $deposit) { ?>
                            <tr>
                                <td>
                                    <?= \yii\helpers\Html::radio('deposit',false,['data'=>['type'=>'coupon'],'value'=>$deposit->id,'class'=>'input-radiolist'])?><?=$deposit->ordersItem->name?>-<?=$deposit->money?>
                                </td>
                            </tr>
                            <?php }?>
                        </table>
                    </td>
                </tr>
            <?php } ?>
                <tr>
                    <td>支付密码：</td>
                    <td style="padding-top:15px;"><?=$form->field($AccountRecord,'pay_password',['template' => '{input}{error}{hint}'])->passwordInput(['style' => 'width:180px', 'class' => 'input-text'])?></td>
                </tr>
                <tr>
                    <td>总计：</td>
                    <td id="final_amount">
                        <?php if($p_cash > 0){?>
                            扣除<?=$p_name?>:<?=number_format($p_cash,2)?> <br>
                        <?php }?>
                        <?php if($cash > 0){?>
                            扣除余额:<?=number_format($cash,2)?> <br>
                         <?php } ?>
                        <?php if(($model->total_amount - ($p_cash+$cash)) > 0) {?>
                            需支付现金：<?=number_format((int)($model->total_amount-$cash-$p_cash),2) ?>
                        <?php } ?>
                    </td>
                </tr>
           <tbody id="paymethod" style="display: <?php if($model->total_amount-$cash-$p_cash >0) echo ''; else echo 'none'?>">
            <tr>
                <td>现金支付</td>
                <td style="padding-top:15px;"><?=$form->field($AccountRecord,'pay_cash',['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'class' => 'input-text'])?></td>
            </tr>
            <tr>
                <td>微信支付</td>
                <td style="padding-top:15px;"><?=$form->field($AccountRecord,'pay_wxpay',['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'class' => 'input-text'])?></td>
            </tr>
            <tr>
                <td>支付宝支付</td>
                <td style="padding-top:15px;"><?=$form->field($AccountRecord,'pay_alipay',['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'class' => 'input-text'])?></td>
            </tr>
            <tr>
                <td>刷卡支付</td>
                <td style="padding-top:15px;"><?=$form->field($AccountRecord,'pay_card',['template' => '{input}{error}{hint}'])->textInput(['style' => 'width:180px', 'class' => 'input-text'])?></td>
            </tr>
            <tr style="display: none">
                <td></td>
                <td><?=$form->field($AccountRecord,'cash',['template' => '{input}{error}{hint}'])->hiddenInput(['style' => 'width:180px', 'class' => 'input-text','value'=>$model->total_amount-$cash-$p_cash])?></td>
            </tr>
            <tr style="display: none">
                <td></td>
                <td><?=$form->field($AccountRecord,'p_cash',['template' => '{input}{error}{hint}'])->hiddenInput(['style' => 'width:180px', 'class' => 'input-text','value'=>$p_cash])?></td>
            </tr>
           </tbody>
            </table>
            <?=Html::hiddenInput('order_id',$model->id,['id'=>'order_id'])?>
            <?=Html::hiddenInput('customer_id',$model->customer_id,['id'=>'customer_id'])?>
            <?=Html::hiddenInput('discount_type',null,['id'=>'discount_type'])?>
        <div class="yx-submit-box" style="width: 100%"><?= Html::submitButton('提交', ['id' => 'dosubmit','class'=>'button yx-submit']) ?></div>
        <?php ActiveForm::end(); ?>
    </div>
</div>
<?php
LayerAsset::register($this);
$js=<<<JS

    $('input[type="checkbox"]').change(function(){
        var val = $(this).val();
        if($(this).is(':checked'))
            checked = 1;
        else 
            checked = 0;
         var order_id = $('#order_id').val();  
         var customer_id = $('#customer_id').val();
         var discount = 0;
         if(typeof($('input[type="radio"]:checked').val()) != 'undefined'){
             discount = $('input[type="radio"]:checked').val();
         }
         var discount_type = $('#discount_type').val();
         
         if(val > 0){
            $.ajax({
                 url:'/customer-cash',
                 type:'post',
                 data:{order_id:order_id,customer_id:customer_id,checked:checked,discount:discount,discount_type:discount_type},
                 success:function(data){
                     if(data.status == 1){
                         $('#final_amount').html(data.final_amount);
                     }
                     if(data.is_bond == 1){
                       $('#is_bond').css('display','table-row');
                       $('#is_deposit').css('display','table-row');
                     }else {
                      $('#is_bond').css('display','none');
                      $('#is_deposit').css('display','none');
                    }
                    $('#accountrecord-cash').val(data.total);
                     if(data.total > 0){
                         $('#paymethod').show();
                     }else{
                         $('#paymethod').css('display','none');
                     }
                 }
            });
        }
        
        
    });
    $('input[name="discount"]').change(function(){
        var type = $(this).data('type');
        var order_id = $('#order_id').val();  
        var customer_id = $('#customer_id').val();
        var val = $(this).val();
        var cash = 0;
        if(typeof($('input[type="checkbox"]:checked').val()) != 'undefined'){
             cash = $('input[type="checkbox"]:checked').val();
         }
        $('#discount_type').val(type);
        $.ajax({
             url:'/customer-discount',
             type:'post',
             data:{order_id:order_id,customer_id:customer_id,type:type,val:val,cash:cash},
             success:function(data){
                 if(data.status == 1){
                         $('#final_amount').html(data.final_amount);
                   }
                 if(data.is_bond == 1){
                     $('#is_bond').css('display','');
                     $('#is_deposit').css('display','');
                 }else {
                      $('#is_bond').css('display','none');
                      $('#is_deposit').css('display','none');
                 }
                 $('#accountrecord-cash').val(data.total);
                 $('#accountrecord-p_cash').val(data.p_cashs);
                 if(data.total > 0){
                         $('#paymethod').show();
                 }else{
                     $('#paymethod').css('display','none');
                 }
             }
            });
    });
    $('input[name="deposit"]').change(function(){
        var order_id = $('#order_id').val();  
        var customer_id = $('#customer_id').val();
        var deposit_id = $(this).val();
        var cash = 0;
        if(typeof($('input[type="checkbox"]:checked').val()) != 'undefined'){
             cash = $('input[type="checkbox"]:checked').val();
         }
        $.ajax({
             url:'/customer-turndeposit',
             type:'post',
             data:{order_id:order_id,customer_id:customer_id,deposit_id:deposit_id,cash:cash},
             success:function(data){
                 if(data.status == 1){
                         $('#final_amount').html(data.final_amount);
                   }
                 $('#accountrecord-cash').val(data.total);
                 if(data.total > 0){
                         $('#paymethod').show();
                 }else{
                     $('#paymethod').css('display','none');
                 }
             }
            });
    });
JS;
\Yii::$app->view->registerJs($js);
?>
